package com.xishu.service;

import com.xishu.bo.shop.ShopItemSearchBo;
import com.xishu.config.Constant;
import com.xishu.dao.jpa.*;
import com.xishu.entity.order.*;
import com.xishu.entity.shop.Shop;
import com.xishu.entity.shop.ShopSearchItem;
import com.xishu.util.BeanUtil;
import com.xishu.util.NativeResultProcessUtils;
import com.xishu.util.Tools;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.persistence.Tuple;
import java.util.List;
import java.util.Optional;

public class ShopSearchItemService implements Tools {
    private static Logger logger = LoggerFactory.getLogger(ShopSearchItemService.class);
    private static ShopSearchItemService instance = new ShopSearchItemService();

    private CommonService commonService = CommonService.getInstance();

    private ShopService shopService = ShopService.getInstance();

    private ShopSearchItemService() {
    }

    public static ShopSearchItemService getInstance() {
        return instance;
    }

    /**
     * 初始化数据
     */
    public void init() {
        if (isInitEnd()) {
            logger.info("ShopSearchItemService init end");
            return;
        }

        //按照每一个分店去查询
        commonService.doAll(new Shop(), (shop -> {
            initItem(shop);
            initCatalog(shop);
            initPayType(shop);
            initWaiter(shop);
            initDeleteOrderItem(shop);
        }));

        //插入结束标记
        createInitEndFlag();
    }

    /**
     * 初始化菜品数据
     *
     * @param shop
     */
    public void initItem(Shop shop) {
        logger.info("initItem {}", shop.getId());
        OrderItemMappingDao orderItemMappingDao = BeanUtil.getBean(OrderItemMappingDao.class);
        List<Tuple> tuples = orderItemMappingDao.shopSearchItem(shop.getId());

        tuples.forEach(p -> {
            ShopItemSearchBo report = new ShopItemSearchBo();
            NativeResultProcessUtils.convertTupleToBean(p, report);

            if (isNotEmpty(report.getName_zh()) && isNotEmpty(report.getName_en())) {
                ShopSearchItem shopSearchItem = new ShopSearchItem(shop.getCompanyId(), shop.getId(), report.getName_zh(), report.getName_en(), Constant.SHOP_SEARCH_ITEM);

                try {
                    commonService.createObject(shopSearchItem);
                } catch (Exception e) {
                    logger.error("e", e);
                }
            }
        });
    }

    /**
     * 初始化品类数据
     *
     * @param shop
     */
    public void initCatalog(Shop shop) {
        logger.info("initCatalog {}", shop.getId());
        OrderCatalogMappingDao orderItemMappingDao = BeanUtil.getBean(OrderCatalogMappingDao.class);
        List<Tuple> tuples = orderItemMappingDao.shopSearchItem(shop.getId());

        tuples.forEach(p -> {
            ShopItemSearchBo report = new ShopItemSearchBo();
            NativeResultProcessUtils.convertTupleToBean(p, report);

            if (isNotEmpty(report.getName_zh()) && isNotEmpty(report.getName_en())) {
                ShopSearchItem shopSearchItem = new ShopSearchItem(shop.getCompanyId(), shop.getId(), report.getName_zh(), report.getName_en(), Constant.SHOP_SEARCH_CATALOG);

                try {
                    commonService.createObject(shopSearchItem);
                } catch (Exception e) {
                    logger.error("e", e);
                }
            }
        });
    }

    /**
     * 初始化品类数据
     *
     * @param shop
     */
    public void initPayType(Shop shop) {
        logger.info("initPayType {}", shop.getId());
        OrderPayTypeMappingDao orderItemMappingDao = BeanUtil.getBean(OrderPayTypeMappingDao.class);
        List<Tuple> tuples = orderItemMappingDao.shopSearchItem(shop.getId());

        tuples.forEach(p -> {
            ShopItemSearchBo report = new ShopItemSearchBo();
            NativeResultProcessUtils.convertTupleToBean(p, report);
            if (isNotEmpty(report.getName_zh()) && isNotEmpty(report.getName_en())) {
                ShopSearchItem shopSearchItem = new ShopSearchItem(shop.getCompanyId(), shop.getId(), report.getName_zh(), report.getName_en(), Constant.SHOP_SEARCH_PAY_TYPE);

                try {
                    commonService.createObject(shopSearchItem);
                } catch (Exception e) {
                    logger.error("e", e);
                }
            }
        });
    }

    /**
     * 初始化服务员
     *
     * @param shop
     */
    public void initWaiter(Shop shop) {
        logger.info("initWaiter {}", shop.getId());
        OrderWaiterMappingDao orderItemMappingDao = BeanUtil.getBean(OrderWaiterMappingDao.class);
        List<Tuple> tuples = orderItemMappingDao.shopSearchItem(shop.getId());

        tuples.forEach(p -> {
            ShopItemSearchBo report = new ShopItemSearchBo();
            NativeResultProcessUtils.convertTupleToBean(p, report);

            if (isNotEmpty(report.getName_zh()) && isNotEmpty(report.getName_en())) {
                ShopSearchItem shopSearchItem = new ShopSearchItem(shop.getCompanyId(), shop.getId(), report.getName_zh(), report.getName_en(), Constant.SHOP_SEARCH_WAITER);

                try {
                    commonService.createObject(shopSearchItem);
                } catch (Exception e) {
                    logger.error("e", e);
                }
            }
        });
    }

    /**
     * 初始化删除的菜品
     *
     * @param shop
     */
    public void initDeleteOrderItem(Shop shop) {
        logger.info("initDeleteOrderItem {}", shop.getId());
        DeleteOrderItemDao orderItemMappingDao = BeanUtil.getBean(DeleteOrderItemDao.class);
        List<Tuple> tuples = orderItemMappingDao.shopSearchItem(shop.getId());

        tuples.forEach(p -> {
            ShopItemSearchBo report = new ShopItemSearchBo();
            NativeResultProcessUtils.convertTupleToBean(p, report);

            if (isNotEmpty(report.getName_zh()) && isNotEmpty(report.getName_en())) {
                ShopSearchItem shopSearchItem = new ShopSearchItem(shop.getCompanyId(), shop.getId(), report.getName_zh(), report.getName_en(), Constant.SHOP_SEARCH_DELETE_ITEM);

                try {
                    commonService.createObject(shopSearchItem);
                } catch (Exception e) {
                    logger.error("e", e);
                }
            }
        });
    }

    /**
     * 添加菜品
     *
     * @param orderItemMapping
     */
    public void addItem(OrderItemMapping orderItemMapping) {
        try {
            ShopSearchItem shopSearchItem = new ShopSearchItem(orderItemMapping.getCompanyId(), orderItemMapping.getShopId(), orderItemMapping.getItemName_zh(), orderItemMapping.getItemName_en(), Constant.SHOP_SEARCH_ITEM);
            Optional<ShopSearchItem> shopSearchItemOptional = commonService.searchOneOptional(shopSearchItem);

            if (shopSearchItemOptional.isPresent()) {
                logger.info("exist");
                return;
            }

            commonService.createObject(shopSearchItem);
        } catch (Exception e) {
            logger.error("e", e);
        }
    }

    /**
     * 添加品类
     *
     * @param mapping
     */
    public void addCatalog(OrderCatalogMapping mapping) {
        try {
            Long shopId = mapping.getShopId();
            Shop shop = shopService.findShopById(shopId);
            ShopSearchItem shopSearchItem = new ShopSearchItem(shop.getCompanyId(), mapping.getShopId(), mapping.getCatalogName_zh(), mapping.getCatalogName_en(), Constant.SHOP_SEARCH_CATALOG);
            Optional<ShopSearchItem> shopSearchItemOptional = commonService.searchOneOptional(shopSearchItem);

            if (shopSearchItemOptional.isPresent()) {
                logger.info("exist");
                return;
            }

            commonService.createObject(shopSearchItem);
        } catch (Exception e) {
            logger.error("e", e);
        }
    }

    /**
     * 添加支付方式
     *
     * @param mapping
     */
    public void addPayType(OrderPayTypeMapping mapping) {
        try {
            Long shopId = mapping.getShopId();
            Shop shop = shopService.findShopById(shopId);
            ShopSearchItem shopSearchItem = new ShopSearchItem(shop.getCompanyId(), mapping.getShopId(), mapping.getPayTypeName_zh(), mapping.getPayTypeName_en(), Constant.SHOP_SEARCH_PAY_TYPE);
            Optional<ShopSearchItem> shopSearchItemOptional = commonService.searchOneOptional(shopSearchItem);

            if (shopSearchItemOptional.isPresent()) {
                logger.info("exist");
                return;
            }

            commonService.createObject(shopSearchItem);
        } catch (Exception e) {
            logger.error("e", e);
        }
    }

    /**
     * 添加服务员
     *
     * @param mapping
     */
    public void addWaiter(OrderWaiterMapping mapping) {
        try {
            Long shopId = mapping.getShopId();
            Shop shop = shopService.findShopById(shopId);
            ShopSearchItem shopSearchItem = new ShopSearchItem(shop.getCompanyId(), mapping.getShopId(), mapping.getWaiterName(), mapping.getWaiterName(), Constant.SHOP_SEARCH_WAITER);
            Optional<ShopSearchItem> shopSearchItemOptional = commonService.searchOneOptional(shopSearchItem);

            if (shopSearchItemOptional.isPresent()) {
                logger.info("exist");
                return;
            }

            commonService.createObject(shopSearchItem);
        } catch (Exception e) {
            logger.error("e", e);
        }
    }

    /**
     * 添加删除菜品
     *
     * @param deleteOrderItem
     */
    public void addDeleteOrderItem(DeleteOrderItem deleteOrderItem) {
        try {
            Long shopId = deleteOrderItem.getShopId();
            Shop shop = shopService.findShopById(shopId);
            ShopSearchItem shopSearchItem = new ShopSearchItem(shop.getCompanyId(), deleteOrderItem.getShopId(), deleteOrderItem.getOrderItemName_zh(), deleteOrderItem.getOrderItemName_en(), Constant.SHOP_SEARCH_DELETE_ITEM);
            Optional<ShopSearchItem> shopSearchItemOptional = commonService.searchOneOptional(shopSearchItem);

            if (shopSearchItemOptional.isPresent()) {
                logger.info("exist");
                return;
            }

            commonService.createObject(shopSearchItem);
        } catch (Exception e) {
            logger.error("e", e);
        }
    }


    /**
     * 是否已经初始化完成
     *
     * @return
     */
    public boolean isInitEnd() {
        ShopSearchItem shopSearchItem = new ShopSearchItem();
        shopSearchItem.setCompanyId(0l);
        ShopSearchItem shopSearchItemInDb = commonService.searchOne(shopSearchItem);
        return shopSearchItemInDb != null;
    }

    /**
     * 创建结束标记
     *
     * @throws Exception
     */
    public void createInitEndFlag() {
        try {
            ShopSearchItem shopSearchItem = new ShopSearchItem();
            shopSearchItem.setCompanyId(0l);
            commonService.createObject(shopSearchItem);
        } catch (Exception e) {
            logger.error("e", e);
        }
    }
}
